package org.jsbd.boss.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.jsbd.boss.common.redis.RedisClient;
import org.jsbd.boss.dao.IMiguVideoRetentConfigDAO;
import org.jsbd.boss.domian.MiguMusicRetentConfigBean;
import org.jsbd.boss.domian.MiguVideoRetentConfigBean;
import org.jsbd.boss.domian.channel.ChannelRetentConfig;
import org.jsbd.boss.service.IMiguVideoRetentConfigService;
import org.jsbd.boss.util.CacheKey;
import org.jsbd.boss.util.DateUtil;
import org.jsbd.boss.util.TimeModelUtils;
import org.springframework.stereotype.Service;

import com.alibaba.dubbo.common.utils.StringUtils;

@Service("miguVideoRetentConfigService")
public class MiguVideoRetentConfigServiceImpl implements IMiguVideoRetentConfigService {

	@Resource
	private IMiguVideoRetentConfigDAO miguVideoRetentConfigDao;
	@Resource
	private RedisClient<String, Object> redisClientMigu;
	
	@Override
	public List<MiguVideoRetentConfigBean> getRetentBymarkforPage(Integer id) {
		List<MiguVideoRetentConfigBean> configList = miguVideoRetentConfigDao.getRetentBymarkforPage(id);
		if(configList==null||configList.isEmpty()){
			return new ArrayList<MiguVideoRetentConfigBean>();
		}
		
		Map<Object,Object> successMap=redisClientMigu.getHash(CacheKey.MIGUVIDEO_RENTENT_SUCCESS_NUM_KEY);
		Map<Object,Object> doNumMap=redisClientMigu.getHash(CacheKey.MIGUVIDEO_RETENT_DO_NUM_HASH_KEY);
		
		String sucessPrefixKey=CacheKey.MIGUVIDEO_RENTENT_SUCCESS_NUM_KEY;
		String doPrefixKey=CacheKey.MIGUVIDEO_RETENT_TASK_NUM_DONE;
		String doDay=DateUtil.getCurrentDateStr();
		
		for(MiguVideoRetentConfigBean config : configList){
			// 总量
			Integer totalNum=config.getTotalNum();
			if(totalNum==null){
				totalNum=0;
			}
			if(config.getRetentRate()==null){
				totalNum=0;
			}else{
				totalNum=totalNum*config.getRetentRate()/100;
			}
			
			// 成功量
			String key=sucessPrefixKey+","+config.getChannelId()+","+config.getVersionCode()+","+doDay+","+config.getRetentDayStr();
			String successNum=(String)successMap.get(key);
			if(StringUtils.isBlank(successNum)){
				successNum="0";
			}
			// 下发量
			String doKey=doPrefixKey+config.getChannelId()+"_"+config.getVersionCode()+"_"+doDay+"_"+config.getRetentDayStr();
			Long doNum=(Long)doNumMap.get(doKey);
			if(doNum==null){
				doNum=0L;
			}
			
            int planNum=TimeModelUtils.genMaxNumByTime(Integer.valueOf(totalNum+""), new Date());
			
			String totalNumStr=totalNum+"|"+planNum+"|"+doNum+"|"+successNum;
			config.setTotalNumStr(totalNumStr);
		}
		
		
		return configList;
	}

	@Override
	public void saveRetent(List<MiguVideoRetentConfigBean> list) {
		miguVideoRetentConfigDao.saveRetent(list);
	}

	@Override
	public void deleteRetentBatch(MiguVideoRetentConfigBean mvrcb) {
		miguVideoRetentConfigDao.deleteRetentBatch(mvrcb);
	}

	@Override
	public void deleteByMiguVideoConfig(Integer id) {
		miguVideoRetentConfigDao.deleteByMiguVideoConfig(id);
	}

}
